package com.scqkxx.gamebox.activity.group;

import android.content.ContentValues;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.group.adapter.GroupMemberAdapter;
import com.scqkxx.gamebox.databinding.ActivityGroupApplyListBinding;
import com.scqkxx.gamebox.databinding.ActivityGroupDetailBinding;
import com.scqkxx.gamebox.widget.MySelfCallDialog;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class GroupApplyListActivity extends BaseActivity<ActivityGroupApplyListBinding> {

    private GroupMemberAdapter groupMemberAdapter;
    private List<AVObject> dataList = new ArrayList<>();
    private MySelfCallDialog dialog;
    private AVObject avObject;

    @Override
    public int setLayoutId() {
        return R.layout.activity_group_apply_list;
    }

    @Override
    public void init() {
        binding.titleView.setOnTitleClickListener(mTitleType -> finish());

        groupMemberAdapter = new GroupMemberAdapter(R.layout.item_group_member, dataList);
        binding.rvGroupMembers.setLayoutManager(new LinearLayoutManager(this));
        binding.rvGroupMembers.setAdapter(groupMemberAdapter);

        String groupId = getIntent().getStringExtra("groupId");
        ContentValues contentValues = new ContentValues();
        contentValues.put("groupId", groupId);

        getGroup(groupId);
        getGroupUsers(groupId);

        binding.btInviteMember.setOnClickListener(view -> ActivityUtil.switchTo(this, GroupInviteCodeActivity.class, contentValues));
        binding.btRemoveGroup.setOnClickListener(view -> {
            if (avObject == null) {
                return;
            }
            if (dialog == null) {
                dialog = new MySelfCallDialog(this);
            }
            dialog.show("确定将成员" + avObject.getString("name") + "移除?", new MySelfCallDialog.OnConfirmListener() {
                @Override
                public void onSure() {
                    if (avObject != null) {
                        avObject.put("isDisable", 1);
                        avObject.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(AVException e) {
                                if (e == null) {
                                    ToastUtils.showShortToast(GroupApplyListActivity.this, "移除成功");
                                }
                            }
                        });
                    }
                }
            });
        });
    }

    private void getGroup(String groupId) {
        AVQuery<AVObject> query = AVQuery.getQuery("Group");
        query.whereEqualTo("objectId", Integer.parseInt(groupId));
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException e) {
                if (e == null && avObjects != null && avObjects.size() > 0) {
                    avObject = avObjects.get(0);
                    if (groupMemberAdapter != null)
                        groupMemberAdapter.setBelongUserId(avObject.getInt("belongUserId"));
                }
            }
        });
    }

    private void getGroupUsers(String groupId) {
        AVQuery<AVObject> query = AVQuery.getQuery("GroupUser");
        query.whereEqualTo("groupId", Integer.parseInt(groupId));
        query.whereNotEqualTo("isAudit", "1");//未审核通过
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException e) {
                Log.i("GroupUser", avObjects + "");
                if (e == null && avObjects != null && avObjects.size() > 0) {
                    dataList.clear();
                    dataList.addAll(avObjects);
                    if (groupMemberAdapter != null)
                        groupMemberAdapter.setNewData(dataList);
                    binding.ivEmpty.setVisibility(dataList.size() == 0 ? View.VISIBLE : View.GONE);
                }
            }
        });
    }

}
